package com.gimi.cloud.bbp.manage.service.app.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.Page;
import com.gimi.cloud.bbp.commonService.common.JmOperationLogService;
import com.gimi.cloud.bbp.dao.dao.homePage.AdDao;
import com.gimi.cloud.bbp.dao.dao.homePage.ColumnDao;
import com.gimi.cloud.bbp.dto.app.AdDTO;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.entity.app.AdEntity;
import com.gimi.cloud.bbp.entity.app.ColumnEntity;
import com.gimi.cloud.bbp.manage.service.app.AdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @auther loongs
 * @date 2017/12/27  15:43
 */
@Service
public class AdServiceImpl implements AdService {
    private AdDao adDao;
    private ColumnDao columnDao;
    private JmOperationLogService jmOperationLogService;
    @Autowired
    public AdServiceImpl(AdDao adDao,ColumnDao columnDao,JmOperationLogService jmOperationLogService) {
        this.adDao = adDao;
        this.columnDao=columnDao;
        this.jmOperationLogService=jmOperationLogService;
    }

    @Override
    public Page<AdDTO> getAdList(AdDTO adDTO) {
        PageHelper.startPage(adDTO.getCurrentPage(), adDTO.getPageSize());
        List<AdDTO> result=adDao.queryPageList(adDTO);
        Page<AdDTO> page =new Page(new PageInfo<>(result));
        return page;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean addAd(AdDTO adDTO) {
        //校验栏目是否存在
        ColumnEntity columnEntity=columnDao.selectByPrimaryKey(adDTO.getColumnId());
        if(columnEntity == null){
            throw new MsgException(ResponseCodeEnum.COLUMN_IS_NOT_EXIT);
        }
        //校验，如果已存在的广告名不能再添加或者修改成为此广告
        AdEntity adEntityTemp= adDao.queryBy("ad_name", adDTO.getAdName());
        if(adEntityTemp != null){
            throw new MsgException(ResponseCodeEnum.AD_IS_EXIT);
        }
        AdEntity adEntity=new AdEntity();
        BeanUtils.copyProperties(adDTO,adEntity);
        //初始化的广告位待发布状态
        adEntity.setAdStatus("c");
        Integer flag=adDao.insertSelective(adEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(adDTO,baseDTO);
            jmOperationLogService.insertAddLog(baseDTO,adDTO.getId()+"","addAd","jimi_ad","添加广告");
            return true;
        }
        return false;
    }

    @Override
    public AdEntity gotoEditAd(AdDTO adDTO) {
        AdEntity adEntity= adDao.selectByPrimaryKey(adDTO.getId());

        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(),"gotoEditAd","jimi_ad",adDTO.getId()+"","转跳到指定广告界面");
        return adEntity;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean editAd(AdDTO adDTO) {
        //校验栏目是否存在
        ColumnEntity columnEntity=columnDao.selectByPrimaryKey(adDTO.getColumnId());
        if(columnEntity == null){
            throw new MsgException(ResponseCodeEnum.COLUMN_IS_NOT_EXIT);
        }
        //校验，如果已存在的广告名不能再添加或者修改成为此广告
        AdEntity adEntityTemp= adDao.queryBy("ad_name", adDTO.getAdName());
        if(adEntityTemp != null && adEntityTemp.getId().intValue() != adDTO.getId().intValue()){
            throw new MsgException(ResponseCodeEnum.AD_IS_EXIT);
        }

        AdEntity adEntity=new AdEntity();
        BeanUtils.copyProperties(adDTO,adEntity);
        adEntity.setModifyTime(new Date());
        Integer flag=adDao.updateByPrimaryKey(adEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(adDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"editAd","jimi_ad",adEntity.getId()+"","编辑指定广告");
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean deleteAd(AdDTO adDTO) throws MsgException {
        AdEntity adEntity=adDao.selectByPrimaryKey(adDTO.getId());
        if(adEntity == null){
            throw new MsgException(ResponseCodeEnum.AD_IS_NOT_EXIT);
        }
        Integer flag=adDao.deleteByPrimaryKey(adEntity);
        if(flag>0){
            //添加操作日志
            jmOperationLogService.insertDeleteLog(adDTO.getAddUid(),"deleteAd","jimi_ad",adDTO.getId()+"","删除指定广告");
            return true;
        }
        return false;
    }

    @Override
    public boolean underAd(AdDTO adDTO) throws MsgException {
        AdEntity adEntity=adDao.selectByPrimaryKey(adDTO.getId());
        //处于已发布的广告才能撤下
        if(adEntity == null || !"d".equals(adEntity.getAdStatus())){
            throw new MsgException(ResponseCodeEnum.AD_IS_NOT_EXIT_OR_PUBLISH);
        }
        adEntity.setAdStatus("e");
        adEntity.setModifyTime(new Date());
        Integer flag=adDao.updateByPrimaryKey(adEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(adDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"underAd","jimi_ad",adEntity.getId()+"","撤下广告");
            return true;
        }
        return false;
    }

    @Override
    public boolean publishAd(AdDTO adDTO) throws MsgException {
        AdEntity adEntity=adDao.selectByPrimaryKey(adDTO.getId());
        //处于待发布和撤下的广告可以发布
        if(adEntity == null || (!"c".equals(adEntity.getAdStatus()) &&  !"e".equals(adEntity.getAdStatus()))){
            throw new MsgException(ResponseCodeEnum.AD_IS_NOT_EXIT_OR_LOAODING_OR_UNDER);
        }
        adEntity.setAdStatus("d");
        adEntity.setPublishMan(adDTO.getAddUid());
        adEntity.setPublishTime(adDTO.getPublishTime());
        adEntity.setEndTime(adDTO.getEndTime());
        adEntity.setModifyTime(new Date());
        Integer flag=adDao.updateByPrimaryKey(adEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(adDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"publishAd","jimi_ad",adEntity.getId()+"","发布广告");
            return true;
        }
        return false;
    }


}
